package com.zyq.service.impl;

import afu.org.checkerframework.checker.oigj.qual.O;
import afu.org.checkerframework.checker.units.qual.A;
import com.github.pagehelper.PageHelper;
import com.zyq.mapper.*;
import com.zyq.model.entity.*;
import com.zyq.model.request.UserDto;
import com.zyq.model.request.UserRequest;
import com.zyq.model.response.DoctorsResponse;
import com.zyq.model.response.HomePageUser10Response;
import com.zyq.model.response.UserResponse;
import com.zyq.model.response.UserStatusResponse;
import com.zyq.service.IUserService;
import com.zyq.util.EmailUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * @author dolyw.com
 * @date 2018/8/9 15:45
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserDto> implements IUserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GetLikeAndCollectMapper getLikeAndCollectMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private HealthInformationMapper healthInformationMapper;

    @Autowired
    private RiskLevelMapper riskLevelMapper;

    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private BloodSugarMapper bloodSugarMapper;

    /**
     * 用户点赞文章
     * @param userId
     * @param articleId
     */
    @Override
    public int insertGetLike(String userId, String articleId){
        return getLikeAndCollectMapper.insertGetLike(userId, articleId);
    }

    /**
     * 取消用户点赞文章
     * @param userId
     * @param articleId
     */
    @Override
    public int deleteGetLike(String userId, String articleId){
        return getLikeAndCollectMapper.deleteGetLike(userId, articleId);
    }

    /**
     * 用户收藏文章
     * @param userId
     * @param articleId
     */
    @Override
    public int collectArticle(String userId, String articleId){
        return getLikeAndCollectMapper.collectArticle(userId, articleId);
    }

    /**
     * 取消用户点赞文章
     * @param userId
     * @param articleId
     */
    @Override
    public int cancelCollectionArticle(String userId, String articleId){
        return getLikeAndCollectMapper.cancelCollectionArticle(userId, articleId);
    }

    /**
     * 查看关注
     * @param userId
     * @param attentionId
     * @return
     */
    @Override
    public int flagAttention(Long userId, Long attentionId){
        return getLikeAndCollectMapper.flagAttention(userId,attentionId);
    }

    /**
     * 关注
     */
    @Override
    public int insertAttention( String userId,  String attentionId){
        return getLikeAndCollectMapper.insertAttention(userId,attentionId);
    }

    /**
     * 取关
     */
    @Override
    public int deleteAttention( String userId,  String attentionId){
        return getLikeAndCollectMapper.deleteAttention(userId,attentionId);
    }

    /**
     * 查找用户状态
     * @param account
     * @return
     */
    @Override
    public int getStatusByAccount(String account){
        return userMapper.getStatusByAccount(account);
    }

    /**
     * 插入用户信息
     * @param user
     */
    @Override
    public int insertUser(User user){
        userMapper.insertUser(user);
        HealthInformation healthInformation = new HealthInformation()
                .setUserId(user.getId())
                .setUserName(user.getUsername());
        return healthInformationMapper.insertHealthInformation(healthInformation);

    }


    /**
     * 查询需要审核的用户
     * @return
     */
    @Override
    public List<UserStatusResponse> getUnAuthorizedUserList(Long status){
        if(status == null)
            return userMapper.getUnAuthorizedUser();
        else{
            return userMapper.getUnAuthorizedUserList(status);
        }
    }

    /**
     * 更新用户状态
     */
    @Override
    public int updateStatus(User user){
        Long status = user.getStatus();
        if(status == 1){
            EmailUtil.sendEmail(user.getEmail(), "尊敬的"+user.getUsername()+"用户,恭喜注册成功");
        }
        else{
            EmailUtil.sendEmail(user.getEmail(), "尊敬的"+user.getUsername()+"用户,你提供的凭证不准确，审核未通过，请重新提供凭证注册！");
        }
        return userMapper.updateStatus(user);
    }


    /**
     * 加入用户角色表
     * @param userId
     * @param roleId
     */
    @Override
    public int insertUserRole(Long userId, Long roleId){
        return userRoleMapper.insertUserRole(userId,roleId);
    }

    /**
     * 插入(更新)用户健康信息
     * @param score
     */
    @Override
    public RiskLevel updateHealthAssessmentByUser(Long score, User user) {
        HealthInformation healthInformation = healthInformationMapper.selectHealthInformationByUserId(user.getId());
        RiskLevel riskLevel = riskLevelMapper.selectLevelByScore(score);
        if(healthInformation!=null){
            if(riskLevel != null) {
                healthInformation.setScore(score)
                        .setLevel(riskLevel.getLevel())
                        .setTips(riskLevel.getTips());
            }
            healthInformationMapper.updateHealthInformation(healthInformation);
        }
        else {
            if(riskLevel != null) {
                healthInformation.setLevel(riskLevel.getLevel())
                        .setScore(score)
                        .setTips(riskLevel.getTips())
                        .setUserId(user.getId())
                        .setUserName(user.getUsername());
                healthInformationMapper.insertHealthInformation(healthInformation);
            }
        }
        return riskLevel;
    }

    /**
     * 获取用户的所有信息
     * @param userResponse
     * @return
     */
    @Override
    public void getUserAll(UserResponse userResponse){
        long userId = userResponse.getBasicUserInformation().getId();
        String role = roleMapper.gerRoleByUserId(userId);

        userResponse.setTopics(topicMapper.selectTopicListByUserId(userId))
                .setCollectArticles(articleMapper.getCollectArticles(userId))
                .setUserRole(role);
        HealthInformation healthInformation = healthInformationMapper.selectHealthInformationByMyself(userId);
        if(role!=null && role.equals("医生")) {
            userResponse.setArticles(articleMapper.selectArticleListByAdmin(new Article().setUserId(userId)));
        }
        if(healthInformation == null){
            userResponse.setHealthInformation(new HealthInformation());
        }else{
            userResponse.setHealthInformation(healthInformation);
        }
        if(role!=null && role.equals("糖尿病患者")) {
            userResponse.setBloodSugars(bloodSugarMapper.select7dayList(userId));
        }
    }

    /**
     * 查找医生
     * @return
     */
    @Override
    public List<DoctorsResponse> recommendDoctors(int page){
        return userMapper.recommendDoctors();
    }

    /**
     * 获取所有用户的信息
     * @param
     * @return
     */
//    @Override
//    public List<UserRequest> selectAllUserAndRole(){
//        return userMapper.selectAllUserAndRole();
//    }

    /**
     * 通过id查找用户信息
     * @param id
     * @return
     */
    @Override
    public UserDto selectById(Integer id){
        return userMapper.selectById(id);
    }

    /**
     * 通过用户id查找粉丝id
     */
    @Override
    public List<Long> selectFans(Long id){
        return userMapper.selectFans(id);
    }

    /**
     * 今日之星
     * @return
     */
    @Override
    public List<HomePageUser10Response> select10(){
        return userMapper.select10();
    }
}
